const state = {
  params: []
}

const mutations = {
  setParams (state, params) {
    state.params = params
  },
  pushParam (state, param) {
    state.params.unshift(param)
  },
  pushSecondParam (state, params) { // 添加二级分类
    let index = state.params.findIndex((value, index, arr) => {
      return value.id === params.one.id
    })
    state.params[index].f_paramvalues.unshift(params.two)
  },
  updataParam (state, param) {
    let index = state.params.findIndex((value, index, arr) => {
      return parseInt(value.id) === parseInt(param.id)
    })
    state.params[index].name = param.name
  },
  updataSecondParam (state, params) {
    let index = state.params.findIndex((value, index, arr) => {
      return parseInt(value.id) === parseInt(params[0].id)
    })
    let twoindex = state.params[index].f_paramvalues.findIndex((value, index, arr) => {
      return parseInt(value.id) === parseInt(params[1].id)
    })
    state.params[index].f_paramvalues[twoindex].name = params[1].name
  },

  deleteParam (state, param) {
    let index = state.params.findIndex((value, index, arr) => {
      return parseInt(value.id) === parseInt(param.id)
    })
    state.params.splice(index, 1)
  },
  deleteSecondParam (state, params) {
    let index = state.params.findIndex((value, index, arr) => {
      return parseInt(value.id) === parseInt(params[0].id)
    })
    let twoindex = state.params[index].f_paramvalues.findIndex((value, index, arr) => {
      return parseInt(value.id) === parseInt(params[1].id)
    })
    state.params[index].f_paramvalues.splice(twoindex, 1)
  }
}

const actions = {
  setParams ({ commit }, params) {
    commit('setParams', params)
  },
  pushParam ({ commit }, param) {
    commit('pushParam', param)
  },
  pushSecondParam ({ commit }, params) {
    commit('pushSecondParam', params)
  },
  updataParam ({ commit }, param) {
    commit('updataParam', param)
  },
  updataSecondParam ({ commit }, params) {
    commit('updataSecondParam', params)
  },
  deleteParam ({ commit }, param) {
    commit('deleteParam', param)
  },
  deleteSecondParam ({ commit }, params) {
    commit('deleteSecondParam', params)
  }
}

export default {
  state,
  mutations,
  actions
}
